﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using FinalProject.BL;
using FinalProject.DAL.Model;

namespace FinalProject.UI
{
    /// <summary>
    /// Interaction logic for CustomerPage.xaml
    /// </summary>
    public partial class CustomerPage : UserControl, IBasePage
    {
        private bool _isLoaded = false;
        public enum FormMode { NewMode, UpdateMode, Normal };
        public FormMode FormState { get; private set; }

        public CustomerPage()
        {
            this.InitializeComponent();
            this.InitializeEvents();
            this.grvCustomer.RefreshData();
            if (this.grvCustomer.HasItems)
                this.ChangeToNormalMode();
            else
                this.ChangeToNewMode();
        }

        #region **Events**

        private void InitializeEvents()
        {
            this.Loaded += CustomerPage_Loaded;
            this.grvCustomer.SelectionChanged += new SelectionChangedEventHandler(grvCustomer_SelectionChanged);

            this.txtCustomerName.PreviewTextInput += new TextCompositionEventHandler(TextUserChanged);
            this.txtCustomerCode.PreviewTextInput += new TextCompositionEventHandler(TextUserChanged);
            this.txtThirdPerson.PreviewTextInput += new TextCompositionEventHandler(TextUserChanged);
            this.txtPhoneNum.PreviewTextInput += new TextCompositionEventHandler(TextUserChanged);
            this.txtFaxNum.PreviewTextInput += new TextCompositionEventHandler(TextUserChanged);
            this.txtIBAN.PreviewTextInput += new TextCompositionEventHandler(TextUserChanged);
            this.txtMaxDebit.PreviewTextInput += new TextCompositionEventHandler(TextUserChanged);
            this.txtTaxCode.PreviewTextInput += new TextCompositionEventHandler(TextUserChanged);
            this.txtAddress.PreviewTextInput += new TextCompositionEventHandler(TextUserChanged);
        }

        private void CustomerPage_Loaded(object sender, RoutedEventArgs e)
        {
            this._isLoaded = true;
        }

        private void grvCustomer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!this._isLoaded) return;
            Customer selectedCustomer = this.grvCustomer.GetSelectedValue();
            this.LoadData(selectedCustomer);
        }


        private void TextUserChanged(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (FormState != FormMode.NewMode)
                FormState = FormMode.UpdateMode;
            Utilities.SwichToEditMode(this, true);
        }

        #endregion

        #region Private Method

        private void LoadData(Customer customer)
        {
            if (customer != null)
            {
                this.txtCustomerCode.Text = customer.Code;
                this.txtCustomerName.Text = customer.Name;
                this.txtThirdPerson.Text = customer.Delegate;
                this.txtIBAN.Text = customer.IBAN;
                this.txtMaxDebit.Text = customer.MaxDebit.ToString();
                this.txtTaxCode.Text = customer.TaxCode;

                this.txtAddress.Text = customer.Address;
                if (!string.IsNullOrEmpty(customer.PhoneNumber))
                    this.txtPhoneNum.Text = customer.PhoneNumber.ToString();
                if (!string.IsNullOrEmpty(customer.FaxNumber))
                    this.txtFaxNum.Text = customer.FaxNumber.ToString();

            }
        }

        private void LoadData(Guid selectedId)
        {
            Customer customer = CustomerBL.LoadById(selectedId);
            this.LoadData(customer);
        }

        private void ResetValuesControl()
        {
            this.txtCustomerCode.Text = string.Empty;
            this.txtCustomerName.Text = string.Empty;
            this.txtThirdPerson.Text = string.Empty;
            this.txtIBAN.Text = string.Empty;
            this.txtTaxCode.Text = string.Empty;

            this.txtAddress.Text = string.Empty;
            this.txtPhoneNum.Text = string.Empty;
            this.txtFaxNum.Text = string.Empty;
        }

        private bool ValidateData()
        {
            bool result = true;
            string defaultMessage = "This field cannot be empty";
            if (string.IsNullOrWhiteSpace(this.txtCustomerCode.Text))
            {
                result = false;
                FormValidation.DisplayWarning(this.txtCustomerCode, defaultMessage);
            }
            if (this.FormState == FormMode.NewMode &&
                CustomerBL.IsExistedCode(this.txtCustomerCode.Text))
            {
                result = false;
                FormValidation.DisplayWarning(this.txtCustomerCode, "This code is existed");
            }
            if (string.IsNullOrWhiteSpace(this.txtTaxCode.Text))
            {
                result = false;
                FormValidation.DisplayWarning(this.txtTaxCode, defaultMessage);
            }
            return result;
        }

        private Customer GetCustomerInfo()
        {
            Customer customer = null;
            if (FormState == FormMode.NewMode)
            {
                customer = new Customer();
                customer.Code = this.txtCustomerCode.Text;
            }
            else
                customer = this.grvCustomer.GetSelectedValue();

            customer.Name = this.txtCustomerName.Text;
            customer.Delegate = this.txtThirdPerson.Text;
            customer.MaxDebit = this.txtMaxDebit.Value;
            customer.IBAN = this.txtIBAN.Text;
            customer.TaxCode = this.txtTaxCode.Text;
            customer.Address = this.txtAddress.Text;
            customer.PhoneNumber = this.txtPhoneNum.Text;
            customer.FaxNumber = this.txtFaxNum.Text;
            return customer;
        }

        #endregion

        #region Public Method

        public void SaveData()
        {
            if (this.ValidateData())
            {
                Customer customer = this.GetCustomerInfo();
                bool success = CustomerBL.InsertOrUpdate(customer);
                if (success)
                {
                    this.ChangeToNormalMode();
                    this.grvCustomer.RefreshData();
                    Utilities.SwichToEditMode(this, false);
                }
            }
        }

        public void CancelModified()
        {
            Utilities.SwichToEditMode(this, false);
            this.ChangeToNormalMode();
            this.LoadData(this.grvCustomer.GetSelectedValue());
        }

        public void ChangeToNewMode()
        {
            this.FormState = FormMode.NewMode;
            this.ResetValuesControl();
            this.txtCustomerCode.IsReadOnly = false;
            this.MainTabBottom.IsEnabled = false;
        }

        public void ChangeToNormalMode()
        {
            this.FormState = FormMode.Normal;
            this.txtCustomerCode.IsReadOnly = true;
            this.MainTabBottom.IsEnabled = true;
        }

        #endregion
    }
}